package org.jvending.provisioning.impl;

import org.jvending.provisioning.dao.DeliveryEventDAO;
import org.jvending.provisioning.model.deliveryevent.DeviceCapability;

import javax.provisioning.Capabilities;
import javax.provisioning.DeliveryComponent;
import javax.provisioning.DeliveryContext;
import javax.provisioning.ProvisioningException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.logging.Logger;

class DeliveryComponentImpl extends DeliveryComponent {

    private static Logger logger = Logger.getLogger("DeliveryComponentImpl");

    private DeliveryEventDAO deliveryEventDAO;

    DeliveryComponentImpl(DeliveryEventDAO deliveryEventDAO) {
        this.deliveryEventDAO = deliveryEventDAO;
    }

    public void handleDeliveryEvent(javax.provisioning.DeliveryEvent deliveryEvent) throws ProvisioningException {
        super.handleDeliveryEvent(deliveryEvent);
        org.jvending.provisioning.model.deliveryevent.DeliveryEvent deliveryEventObject =
                translateDeliveryEventToModel(deliveryEvent);
        try {
            deliveryEventDAO.store(deliveryEventObject);
        } catch (IOException e) {
            e.printStackTrace();
            logger.info("JV-1500-44: Unable store delivery event: " + deliveryEvent);
        }
    }

    org.jvending.provisioning.model.deliveryevent.DeliveryEvent
            translateDeliveryEventToModel(javax.provisioning.DeliveryEvent deliveryEvent) {
        org.jvending.provisioning.model.deliveryevent.DeliveryEvent deliveryEventObject =
                new org.jvending.provisioning.model.deliveryevent.DeliveryEvent();

        DeliveryContext deliveryContext = deliveryEvent.getDeliveryContext();
        String networkId = deliveryContext != null ? deliveryContext.getNetworkID() : "UNKNOWN";
        String userId = deliveryContext != null ? deliveryContext.getUser() : "UNKNOWN";

        Capabilities capabilities = deliveryContext != null ? deliveryContext.getDeviceCapabilities() :
                ProvisioningFactory.createEmptyCapabilities();
        List<DeviceCapability> capabilityList = new ArrayList<DeviceCapability>();

        Set capabilityNames = capabilities.getCapabilityNames();
        for (Iterator i = capabilityNames.iterator(); i.hasNext();) {
            String name = (String) i.next();
            List capabilityValues = capabilities.getCapability(name);
            for (Iterator j = capabilityValues.iterator(); j.hasNext();) {
                String value = (String) j.next();
                DeviceCapability deviceCapability = new DeviceCapability();
                deviceCapability.setName(name);
                deviceCapability.setValue(value);
                capabilityList.add(deviceCapability);
            }
        }
/*
            public static int COMPLETE = 0;

            public static int CONFIRMED = 1;

            public static int DELETED = 2;

            public static int FAILED = 3;

            public static int INVALID_FULFILLMENT_ID = 0;

            public static int OTHER = 5;


            String code;
            switch(deliveryEvent.getCode()) {

            }
            */
        deliveryEventObject.setAdapterName(deliveryEvent.getAdapterName());
        //deliveryEventObject.setCode(deliveryEvent.getCode());
        deliveryEventObject.setDescription(deliveryEvent.getDescription());
        deliveryEventObject.setDeviceCapabilities(capabilityList);
        deliveryEventObject.setFulfillmentId(deliveryEvent.getFulfillmentID());
        deliveryEventObject.setEventTime(System.currentTimeMillis());
        deliveryEventObject.setNetworkId(networkId);
        //deliveryEventObject.setType();
        deliveryEventObject.setUserId(userId);
        return deliveryEventObject;
    }
}
